package com.ruoyi.training.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.training.domain.A05ResTextbook;
import com.ruoyi.training.service.IA05ResTextbookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 教材资源Controller
 * 
 * @author WorrilessGo
 * @date 2025-11-18
 */
@RestController
@RequestMapping("/textbook")
public class A05ResTextbookController extends BaseController
{
    @Autowired
    private IA05ResTextbookService a05ResTextbookService;

    /**
     * 查询教材资源列表
     */
    @RequiresPermissions("training:textbook:list")
    @GetMapping("/list")
    public TableDataInfo list(A05ResTextbook a05ResTextbook)
    {
        startPage();
        List<A05ResTextbook> list = a05ResTextbookService.selectA05ResTextbookList(a05ResTextbook);
        return getDataTable(list);
    }

    /**
     * 导出教材资源列表
     */
    @RequiresPermissions("training:textbook:export")
    @Log(title = "教材资源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, A05ResTextbook a05ResTextbook)
    {
        List<A05ResTextbook> list = a05ResTextbookService.selectA05ResTextbookList(a05ResTextbook);
        ExcelUtil<A05ResTextbook> util = new ExcelUtil<A05ResTextbook>(A05ResTextbook.class);
        util.exportExcel(response, list, "教材资源数据");
    }

    @PostMapping("/exportModel")
    public void exportModel(HttpServletResponse response)
    {
        List<A05ResTextbook> list = new ArrayList<>();
        ExcelUtil<A05ResTextbook> util = new ExcelUtil<A05ResTextbook>(A05ResTextbook.class);
        util.exportExcel(response, list, "教材资源数据");
    }

    /**
     * 导入教材资源数据
     */
    @RequiresPermissions("training:textbook:import")
    @Log(title = "教材资源", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<A05ResTextbook> util = new ExcelUtil<A05ResTextbook>(A05ResTextbook.class);
        List<A05ResTextbook> list = util.importExcel(file.getInputStream(),1);
        int message = a05ResTextbookService.batchInsertA05ResTextbook(list);
        return AjaxResult.success(message);
    }


    /**
     * 获取教材资源详细信息
     */
    @RequiresPermissions("training:textbook:query")
    @GetMapping(value = "/{textbookId}")
    public AjaxResult getInfo(@PathVariable("textbookId") String textbookId)
    {
        return success(a05ResTextbookService.selectA05ResTextbookByTextbookId(textbookId));
    }

    /**
     * 新增教材资源
     */
    @RequiresPermissions("training:textbook:add")
    @Log(title = "教材资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody A05ResTextbook a05ResTextbook)
    {
        return toAjax(a05ResTextbookService.insertA05ResTextbook(a05ResTextbook));
    }

    /**
     * 修改教材资源
     */
    @RequiresPermissions("training:textbook:edit")
    @Log(title = "教材资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody A05ResTextbook a05ResTextbook)
    {
        return toAjax(a05ResTextbookService.updateA05ResTextbook(a05ResTextbook));
    }

    /**
     * 删除教材资源
     */
    @RequiresPermissions("training:textbook:remove")
    @Log(title = "教材资源", businessType = BusinessType.DELETE)
	@DeleteMapping("/{textbookIds}")
    public AjaxResult remove(@PathVariable String[] textbookIds)
    {
        return toAjax(a05ResTextbookService.deleteA05ResTextbookByTextbookIds(textbookIds));
    }
}
